<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="../js/documentMyReady.js"></script>
    <title>Document</title>
    <style>
        h1{
            text-align: center;
        }
        div{
            text-align: center;
            margin: 20px auto
        }
        div input{
            padding: 0 10px;
            line-height:1.5;
            font-size: 20px;     
        }
        div ul.list{
            list-style-type: none;
            padding: 0;
            width: 265px;
            margin: 0 auto;        
            border: 1px solid #ccc;     
            border-top: 0;
            display: none;
        }
        div ul.list li{
            text-indent: 0.5em;
            text-align: left;
            line-height: 1.5;
            font-size: 15px;
        }
        div ul.list li:hover{
            background-color:#eee;
            color: #111;
            cursor: default;
        }
    </style>
</head>
<script>
    
    //当文本框获得焦点时候显示list
    document.myReady(function() {
        //选择dom
        let inputText = document.getElementsByTagName('input')[0];
        let listUl = document.getElementsByClassName('list')[0];
        inputText.onfocus = function(){
            listUl.style.display = 'block';
        }
        inputText.onblur = function(){
            listUl.style.display = 'none';
        }
        //设置li的点击事件
        let listLi = document.getElementsByTagName('li');
        let listLiLength = listLi.length;
        let changeValue = function(){
            inputText.value=this.textContent;
        }
        for(let i = 0; i < listLiLength; i ++){
            listLi[i].addEventListener('mousedown', changeValue, false);        
        //为何不用click事件
        //1：click 事件 执行顺序
        // 1：mousedown --> 原来元素失去焦点 -->点击元素获得焦点 -->  mouseup --> click事件
        }
    });
</script>
<body>
    <h1>文本框提示</h1>
    <div>
        <input type="text">
        <ul class="list">
            <li>文本框提示1</li>
            <li>文本框提示2</li>
            <li>文本框提示3</li>
            <li>文本框提示4</li>
            <li>文本框提示5</li>
        </ul>
    </div>
</body>
</html>